<?php

namespace App\Service\Workbench;

use App\Consts\Approval\ReturnStatus;
use App\Func\AgendaFunc;
use App\Func\StaffFunc;
use App\Func\TokenFuc;
use App\Models\Agenda\AgendaModel;
use App\Models\Client\ClientModel;
use App\Models\modelCreditProjectManage;
use App\Models\NewContract\ContractModel;

class WorkbenchService
{
    public $agendaModel;

    public function __construct()
    {
        $this->agendaModel = new AgendaModel();
    }

    public function getSelfContractCalendar(){
        $data = [];
        // 获取经办人为我的合同
        $staffId = TokenFuc::getCurrentStaffInfo()['staff_id'];
        $contractList = (new ContractModel())->getStaffContractByStaffId($staffId,['contract_id','contract_name','contract_end_time','contract_start_time']);
        if($contractList){
            $contractList = $contractList->toArray();
            foreach($contractList as  $value){

                if(mb_strlen($value['contract_name'])>=5){
                    $str = "-".mb_substr($value['contract_name'],0,5)."...";
                }else{
                    $str = "(".$value['contract_code'].")-".$value['contract_name'];
                }
                if($contractList['contract_start_time']){
                    $data[] = [
                        'remind_str' => "合同开始".$str,
                        'remind_id' => $value['contract_id'],
                        'skip_type' => ReturnStatus::CONTRACT_RETURN,
                        'remind_weight' => 1,
                        'remind_date' => date("Y-m-d",strtotime($value['contract_start_time'])),
                    ];
                }
                if($contractList['contract_end_time']){
                    $data[] = [
                        'remind_str' => "合同截止".$str,
                        'remind_id' => $value['contract_id'],
                        'skip_type' => ReturnStatus::CONTRACT_RETURN,
                        'remind_weight' => 2,
                        'remind_date' => date("Y-m-d",strtotime($value['contract_end_time'])),
                    ];
                }
            }
        }
        return $data;
    }

    public function getSelfAgendaList(mixed $pageSize, mixed $filters)
    {
        $staffId = TokenFuc::getCurrentStaffInfo()['staff_id'];
        $query = $this->agendaModel->select([
            "agenda_id",
            "staff_id",
            "agenda_type",
            "agenda_type as agenda_type_str",
            "agenda_source_id",
            "created_at",
        ])->where('staff_id',$staffId);
        if($filters){
            $filters = json_decode($filters,true);
            if(isset($filters['agenda_type']) && $filters['agenda_type']){
                $query = $query->where('agenda_type',$filters['agenda_type']);
            }
        }
        $res =  $this->agendaModel->getAgendaList($query,$pageSize)->toArray();
        $staffAll = StaffFunc::getStaffAllBySelect();
        foreach($res['data'] as $key => $value){
            $res['data'][$key]['staff_name'] = StaffFunc::getStaffNameByStaffId($staffAll,$value['staff_id']);
            $info = $this->getAgendaType($value['agenda_type'],$value['agenda_source_id']);
            $res['data'][$key]['agenda_source_str'] = $info['str'];
            $res['data'][$key]['skip_type'] = $info['skip_type'];
        }
        return $res;
    }

    private function getAgendaType($type,$sourceId){
        switch ($type){
            case AgendaFunc::AGENDA_TYPE_CLIENT_ADJUST_AMOUNT_CREDIT_CLIENT_JOIN:
                return [
                    'str' => @(new ClientModel())->getClientInfoByClientId($sourceId,["client_name"])["client_name"] ?? "-",
                    'skip_type' => ReturnStatus::CLIENT_RETURN,
                ];
            case AgendaFunc::AGENDA_TYPE_CLIENT_ADJUST_AMOUNT_LAW:
            case AgendaFunc::AGENDA_TYPE_CLIENT_ADJUST_AMOUNT_CREDIT_CONTROL:
            case AgendaFunc::AGENDA_TYPE_CLIENT_ADJUST_AMOUNT_FINANCE:
            case AgendaFunc::AGENDA_TYPE_CLIENT_ADJUST_AMOUNT_DEPT:
            return [
                'str' => @(new ClientModel())->getClientInfoByClientId($sourceId,["client_name"])["client_name"] ?? "-",
                'skip_type' => ReturnStatus::CREDIT_CLIENT_RETURN,
            ];
            case AgendaFunc::AGENDA_TYPE_CREDIT_PROJECT_DEPARTMENT_LEADER_REVIEWING:
            case AgendaFunc::AGENDA_TYPE_CREDIT_PROJECT_CREDIT_MANAGER_REVIEW:
            case AgendaFunc::AGENDA_TYPE_CREDIT_PROJECT_FINANCE_MANAGER_REVIEW:
            case AgendaFunc::AGENDA_TYPE_CREDIT_PROJECT_REVIEWING:
            case AgendaFunc::AGENDA_TYPE_CREDIT_PROJECT_AMOUNT_ADJUST_REVIEW:
            return [
                'str' =>  @(new modelCreditProjectManage())->getSimpleProjectInfoByCreditProjectId($sourceId,"credit_project_name")["credit_project_name"] ?? "-",
                'skip_type' => ReturnStatus::CREDIT_PROJECT_RETURN,
            ];

        }
    }
}